package com.andnnl;

import com.andnnl.dao.base.EmasUcinfoDao;
import com.andnnl.pojo.base.EmasUcinfo;
import com.andnnl.utils.JsonLib;
import org.beetl.sql.core.*;
import org.beetl.sql.core.db.DBStyle;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.core.db.SqlServer2012Style;
import org.beetl.sql.ext.DebugInterceptor;
import org.beetl.sql.ext.gen.GenConfig;
import org.beetl.sql.ext.gen.GenFilter;
import org.beetl.sql.ext.gen.MapperCodeGen;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenss on 2019/5/10.
 */
public class GeneratorMain {
    private String type;

    public static void main(String[] args) throws Exception {
//        genBase();
//        genSms();
//        genMysql();
//        testBaseSelect();
//        testSmsSelect();

//        genMapper();
        genPublic();

    }

    private static void testSmsSelect() throws ParseException {
        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String url = "jdbc:sqlserver://192.168.4.241:1433;DatabaseName=EMAS_SMS";
        String userName = "sa";
        String password = "jy@88250860";
        ConnectionSource source = ConnectionSourceHelper.getSimple(driver, url, userName, password);
        DBStyle mysql = new SqlServer2012Style();
        // sql语句放在classpagth的/sql 目录下
        SQLLoader loader = new ClasspathLoader("/sql/sms");
        // 数据库命名跟java命名一样，所以采用DefaultNameConversion，还有一个是UnderlinedNameConversion，下划线风格的，
//        UnderlinedNameConversion nc = new UnderlinedNameConversion();
        JPA2NameConversion nc = new JPA2NameConversion();
//        DefaultNameConversion nc = new DefaultNameConversion();
        // 最后，创建一个SQLManager,DebugInterceptor 不是必须的，但可以通过它查看sql执行情况
        SQLManager sqlManager = new SQLManager(mysql, loader, source, nc, new Interceptor[]{new DebugInterceptor()});

//        SmsMobilesDao dao=sqlManager.getMapper(SmsMobilesDao.class);
//        Query<SmsMobiles> query = sqlManager.query(SmsMobiles.class);
//        List<SmsMobiles> list = query.andEq("ucuid", "chenss")
//                .andGreatEq("SENDTIME", "2019-05-01")
//                .orderBy("SENDTIME desc")
//                .limit(1, 2)
////                .select(new String[]{"id","mobile"});
//                .select("id","mobile");
//        System.out.println(JsonLib.toJson(list));


//        SmsMobiles sm=new SmsMobiles();
//        sm.setUcuid("chenss");
//        sm.setMinSendTime(new SimpleDateFormat("yyyy-MM-dd").parse("2019-05-01"));
//        list =sqlManager.select("smsMobiles.sample",SmsMobiles.class,sm,1,2);
//        System.out.println(JsonLib.toJson(list));


        //从第一页开始查询,无参数
//        PageQuery pageQuery = new PageQuery();
//        pageQuery.setParas(sm);
//        pageQuery.setPageSize(2);
////        pageQuery.setOrderBy("SENDTIME desc");
//        sqlManager.pageQuery("smsMobiles.sample", SmsMobiles.class,pageQuery);
//        System.out.println(pageQuery.getTotalPage());
//        System.out.println(pageQuery.getTotalRow());
//        System.out.println(pageQuery.getPageNumber());
//        List<SmsMobiles> list2 = pageQuery.getList();
//        System.out.println(JsonLib.toJson(list2));

    }

    private static void genMapper() throws Exception {
        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String url = "jdbc:sqlserver://192.168.4.241:1433;DatabaseName=EMAS_BASE";
        String userName = "sa";
        String password = "jy@88250860";
        ConnectionSource source = ConnectionSourceHelper.getSimple(driver, url, userName, password);
        DBStyle mysql = new SqlServer2012Style();
        // sql语句放在classpagth的/sql 目录下
        SQLLoader loader = new ClasspathLoader("/sql/base");
        // 数据库命名跟java命名一样，所以采用DefaultNameConversion，还有一个是UnderlinedNameConversion，下划线风格的，
        JPA2NameConversion nc = new JPA2NameConversion();
//        UnderlinedNameConversion nc = new UnderlinedNameConversion();
//        DefaultNameConversion nc = new DefaultNameConversion();
        // 最后，创建一个SQLManager,DebugInterceptor 不是必须的，但可以通过它查看sql执行情况
        SQLManager sqlManager = new SQLManager(mysql, loader, source, nc, new Interceptor[]{new DebugInterceptor()});

        MapperCodeGen mapper = new MapperCodeGen("com.jiaying.dao.base");
        GenConfig config = new GenConfig();
        config.preferBigDecimal(true);
        config.setBaseClass("Test1");
        config.codeGens.add(mapper);
//        sqlManager.genPojoCodeToConsole("Test1", config);

    }

    private static void testBaseSelect() {
        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String url = "jdbc:sqlserver://192.168.4.241:1433;DatabaseName=EMAS_BASE";
        String userName = "sa";
        String password = "jy@88250860";
        ConnectionSource source = ConnectionSourceHelper.getSimple(driver, url, userName, password);
        DBStyle mysql = new SqlServer2012Style();
        // sql语句放在classpagth的/sql 目录下
        SQLLoader loader = new ClasspathLoader("/sql/base");
        // 数据库命名跟java命名一样，所以采用DefaultNameConversion，还有一个是UnderlinedNameConversion，下划线风格的，
        JPA2NameConversion nc = new JPA2NameConversion();
//        UnderlinedNameConversion nc = new UnderlinedNameConversion();
//        DefaultNameConversion nc = new DefaultNameConversion();
        // 最后，创建一个SQLManager,DebugInterceptor 不是必须的，但可以通过它查看sql执行情况
        SQLManager sqlManager = new SQLManager(mysql, loader, source, nc, new Interceptor[]{new DebugInterceptor()});

        int rs = 0;
        int[] rss;
        EmasUcinfo query;
        //插入返回ID
//        query = new EmasUcinfo();
//        query.setUid("chenss_beetl");
//        query.setLevels(0);
//        query.setRtnum(0);
//        query.setRtcount(0);
//        query.setFee(new BigDecimal("0"));
//        query.setFei(0);
//        query.setIsagent(0);
//        query.setShownum(0);
//        query.setShowcount(0);
//        query.setIsagentop(0);
//        query.setFailnum(0);
//        query.setFailcount(0);
//        query.setPrice(new BigDecimal("0"));
//        rs=sqlManager.insert(query,true);
//        System.out.println(rs);
//        System.out.println(query.getId());


        //根据ID删除
//        rs=sqlManager.deleteById(EmasUcinfo.class,query.getId());
//        System.out.println(rs);

        if (1 == 1) return;

        //模板查询
        query = new EmasUcinfo();
        query.setUid("chenss");
        List<EmasUcinfo> list = sqlManager.template(query);
        System.out.println(JsonLib.toJson(list));


        //id查询
        query = new EmasUcinfo();
        query.setId(3247);
        list = sqlManager.template(query);
        System.out.println(JsonLib.toJson(list));


        //根据ID更新，自动生成更新代码
        query = new EmasUcinfo();
        query.setId(3247);
        query.setClassInt(2);
        query.setFax("13123");
        rs = sqlManager.updateTemplateById(query);
//        System.out.println("rs->"+rs);


        //直接使用md文件里的SQL语句更新
        Map<String, Object> updateMap = new HashMap<>();
        updateMap.put("id", 3247);
        updateMap.put("fax", "111111");
        updateMap.put("classInt", "0");
        rs = sqlManager.update("emasUcinfo.updateFax", updateMap);
//        System.out.println("rs->"+rs);


        //使用mapper接口，使用md文件里的SQL语句更新
        EmasUcinfoDao dao = sqlManager.getMapper(EmasUcinfoDao.class);
        dao.updateFax(3247, "222222", 0);


        //批量用SQL更新
        List<EmasUcinfo> updateList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            query = new EmasUcinfo();
            query.setId(3247);
            query.setClassInt(2);
            query.setFax("13123");
            updateList.add(query);
        }


        //根据ID批量更新，SQL自动生成
        rss = sqlManager.updateBatchTemplateById(EmasUcinfo.class, updateList);
        System.out.println("根据ID批量更新，SQL自动生成rss->" + rss);


        //使用md文件里的SQL语句批量更新，参数是bean列表
        rss = sqlManager.updateBatch("emasUcinfo.updateFax", updateList);
        System.out.println("使用md文件里的SQL语句批量更新，参数是bean列表rss->" + rss);


        List<Map<String, Object>> updateMapList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Map<String, Object> updateMap2 = new HashMap<>();
            updateMap2.put("id", 3247);
            updateMap2.put("fax", "222222");
            updateMap2.put("classInt", "0");
            updateMapList.add(updateMap2);
        }

        //使用md文件里的SQL语句批量更新，参数是map列表
        rss = sqlManager.updateBatch("emasUcinfo.updateFax", updateMapList);
        System.out.println("使用md文件里的SQL语句批量更新，参数是map列表rss->" + rss);
    }

    private static void genBase() throws Exception {
        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String url = "jdbc:sqlserver://192.168.4.241:1433;DatabaseName=EMAS_BASE";
        String userName = "sa";
        String password = "jy@88250860";
        ConnectionSource source = ConnectionSourceHelper.getSimple(driver, url, userName, password);
        DBStyle mysql = new SqlServer2012Style();
        // sql语句放在classpagth的/sql 目录下

        SQLLoader loader = new ClasspathLoader("/sql/base");
        // 数据库命名跟java命名一样，所以采用DefaultNameConversion，还有一个是UnderlinedNameConversion，下划线风格的，
//        UnderlinedNameConversion nc = new UnderlinedNameConversion();
//        DefaultNameConversion nc = new DefaultNameConversion();
        DefaultNameConversion nc = new DefaultNameConversion();
        // 最后，创建一个SQLManager,DebugInterceptor 不是必须的，但可以通过它查看sql执行情况
        SQLManager sqlManager = new SQLManager(mysql, loader, source, nc, new Interceptor[]{new DebugInterceptor()});


//        sqlManager.genPojoCodeToConsole("TBL_EMAS_UCINFO");
//        sqlManager.genSQLTemplateToConsole("TBL_EMAS_UCINFO","base.ucinfo");
        GenFilter filter = new GenFilter() {
            @Override
            public boolean accept(String tableName) {
//                if (tableName.startsWith("t") || tableName.startsWith("T"))
//                    return true;
                if (tableName.toLowerCase().equals("mms_template"))
                    return true;
                return false;
            }
        };
        GenConfig config = new GenConfig();
        MapperCodeGen mapper = new MapperCodeGen("com.jiaying.dao.base");
        config.codeGens.add(mapper);
//        config.setIgnorePrefix("tbl");
        config.setIgnorePrefix("mms");
        sqlManager.genALL("com.jiaying.pojo.base", config, filter);
    }

    private static void genSms() throws Exception {
        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String url = "jdbc:sqlserver://192.168.4.241:1433;DatabaseName=EMAS_SMS";
        String userName = "sa";
        String password = "jy@88250860";
        ConnectionSource source = ConnectionSourceHelper.getSimple(driver, url, userName, password);
        DBStyle mysql = new SqlServer2012Style();
        // sql语句放在classpagth的/sql 目录下
        SQLLoader loader = new ClasspathLoader("/sql/sms");
        // 数据库命名跟java命名一样，所以采用DefaultNameConversion，还有一个是UnderlinedNameConversion，下划线风格的，
//        UnderlinedNameConversion nc = new UnderlinedNameConversion();
        DefaultNameConversion nc = new DefaultNameConversion();
        // 最后，创建一个SQLManager,DebugInterceptor 不是必须的，但可以通过它查看sql执行情况
        SQLManager sqlManager = new SQLManager(mysql, loader, source, nc, new Interceptor[]{new DebugInterceptor()});


        GenFilter filter = new GenFilter() {
            @Override
            public boolean accept(String tableName) {
//                if (tableName.startsWith("t") || tableName.startsWith("T"))
//                    return true;
                if (tableName.toLowerCase().equals("mms_mo"))
                    return true;
                return false;
            }
        };
        GenConfig config = new GenConfig();
        MapperCodeGen mapper = new MapperCodeGen("com.jiaying.dao.sms");
        config.codeGens.add(mapper);
//        config.setIgnorePrefix("tbl");
        config.setIgnorePrefix("mms");
        sqlManager.genALL("com.jiaying.pojo.sms", config, filter);
    }

    private static void genPublic() throws Exception {
        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String url = "jdbc:sqlserver://192.168.4.241:1433;DatabaseName=EMAS_PUBLIC";
        String userName = "sa";
        String password = "jy@88250860";
        ConnectionSource source = ConnectionSourceHelper.getSimple(driver, url, userName, password);
        DBStyle mysql = new SqlServer2012Style();
        // sql语句放在classpagth的/sql 目录下
        SQLLoader loader = new ClasspathLoader("/sql/public");
        // 数据库命名跟java命名一样，所以采用DefaultNameConversion，还有一个是UnderlinedNameConversion，下划线风格的，
//        UnderlinedNameConversion nc = new UnderlinedNameConversion();
        DefaultNameConversion nc = new DefaultNameConversion();
        // 最后，创建一个SQLManager,DebugInterceptor 不是必须的，但可以通过它查看sql执行情况
        SQLManager sqlManager = new SQLManager(mysql, loader, source, nc, new Interceptor[]{new DebugInterceptor()});


        GenFilter filter = new GenFilter() {
            @Override
            public boolean accept(String tableName) {
                if (tableName.startsWith("account_blacklist"))
                    return true;
//                if (tableName.toLowerCase().equals("mms_mo"))
//                    return true;
                return false;
            }
        };
        GenConfig config = new GenConfig();
        MapperCodeGen mapper = new MapperCodeGen("com.jiaying.dao.public");
        config.codeGens.add(mapper);
//        config.setIgnorePrefix("tbl");
        config.setIgnorePrefix("mms");
        sqlManager.genALL("com.jiaying.pojo.public", config, filter);
    }


    private static void genMysql() throws Exception {
        String driver = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://192.168.4.240:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false&useInformationSchema=true";
        String userName = "root";
        String password = "123456";
        ConnectionSource source = ConnectionSourceHelper.getSimple(driver, url, userName, password);
        DBStyle mysql = new MySqlStyle();
        // sql语句放在classpagth的/sql 目录下
        SQLLoader loader = new ClasspathLoader("/sql/test");
        // 数据库命名跟java命名一样，所以采用DefaultNameConversion，还有一个是UnderlinedNameConversion，下划线风格的，
//        UnderlinedNameConversion nc = new UnderlinedNameConversion();
        DefaultNameConversion nc = new DefaultNameConversion();
        // 最后，创建一个SQLManager,DebugInterceptor 不是必须的，但可以通过它查看sql执行情况
        SQLManager sqlManager = new SQLManager(mysql, loader, source, nc, new Interceptor[]{new DebugInterceptor()});


        GenFilter filter = new GenFilter() {
            @Override
            public boolean accept(String tableName) {
//                if (tableName.startsWith("t") || tableName.startsWith("T"))
//                    return true;
                if (tableName.toLowerCase().equals("user"))
                    return true;
                return false;
            }
        };
        GenConfig config = new GenConfig();
        MapperCodeGen mapper = new MapperCodeGen("com.andnnl.dao.test");
        config.codeGens.add(mapper);
//        config.setIgnorePrefix("tbl");
//        config.setIgnorePrefix("mms");
        sqlManager.genALL("com.andnnl.pojo.test", config, filter);
    }


}
